<template>
  <div class="cert">
    <p class="detail-tip">
      技能证书<span v-if="showView && !isStatic" @click="addList">添加</span>
    </p>
    <div class="com-container" v-if="showView">
      <div class="show-item">
        <span class="name" v-for="v in list" :key="v.id">{{
          v.skill_id_cn || v.name
        }}</span>
      </div>
      <p class="btn-wrap" v-if="!isStatic && list.length !== 0">
        <span class="edit" @click="toEdit">编辑</span>
      </p>
    </div>
    <div class="com-form" v-else>
      <div class="postForm">
        <div class="showEdit" v-if="showDelete">
          <div class="form-item" v-for="(m, n) in list" :key="'editCert_' + n">
            <span class="name">{{ m.skill_id_cn || m.name }}</span>
            <span class="delete" @click="toDelete(n)">删除</span>
          </div>
        </div>
        <div class="form-select" v-else>
          <div class="form-item">
            <div class="text" required>选择类型:</div>
            <div class="select">
              <client-only>
                <el-select
                  v-model="edit.name"
                  placeholder="请选择类型"
                  @change="changeId"
                >
                  <el-option
                    v-for="item in skillList"
                    :key="'skillList_' + item.id"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </client-only>
            </div>
          </div>
          <div class="form-item">
            <div class="text" required>选择技能:</div>
            <div class="select">
              <client-only>
                <el-select
                  v-model="edit.skill_id_cn"
                  placeholder="请选择技能"
                  @change="changeChildId"
                >
                  <el-option
                    v-for="item in skillChildList"
                    :key="'skillChildList_' + item.id"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </client-only>
            </div>
          </div>
        </div>
        <div class="submit">
          <button class="confirm" type="button" @click="submit">保存</button>
          <button class="cancel" type="button" @click="cancel">取消</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getSkill } from '@/api/users'
export default {
  props: ['certList', 'isStatic'],
  data () {
    return {
      showView: true,
      showDelete: false,
      list: [],
      edit: { id: '', name: '', skill_id: '', skill_id_cn: '' },
      skillList: [],
      skillChildList: []
    }
  },
  watch: {
    certList (val) {
      this.list = val;
    }
  },
  created () {
    this.list = this.certList || [];
  },
  mounted () {
   !this.isStatic && this.toGetSkill();
  },
  methods: {
    changeId (data) {
      this.edit.id = data;
      const curOne = this.skillList.find(s => s.id === data) || {};
      this.edit.name = curOne.name || '';
      this.skillChildList = curOne.child || [];
      this.edit.skill_id = '';
      this.edit.skill_id_cn = '';
    },
    changeChildId (data) {
      this.edit.skill_id = data;
      const curOne = this.skillChildList.find(s => s.id === data) || {};
      this.edit.skill_id_cn = curOne.name || '';
    },
    toGetSkill () { // 获取skill
      getSkill({}).then(res => {
        if (res.code === 200) {
          this.skillList = res.data;
        } else {
          this.$message.error(res.msg)
        }
      }).catch(err => {
        console.log(err)
        this.$message.error(err || '网络错误！')
      })
    },
    addList () {
      this.showView = false;
    },
    toggleView () {
      this.showView = false;
    },
    toEdit () {
      this.showDelete = true;
      this.showView = false;
    },
    toDelete (index) {
      this.list.splice(index, 1);
    },
    cancel () {
      if (this.showDelete) {
        this.showDelete = false;
      }
      this.showView = true;
    },
    submit () {
      if (this.showDelete) {
        this.showDelete = false;
      } else {
        this.list.push(this.edit);
        this.edit = { id: '', name: '', skill_id: '', skill_id_cn: '' };
      }
      this.showView = true;
    },
  }
}
</script>
<style lang="scss" scoped>
.cert {
  position: relative;
  .show-item {
    line-height: 40px;
    padding-left: 30px;
    span {
      line-height: 22px;
      display: inline-block;
      padding: 0 15px;
      margin: 0 20px 20px 0;
      border: 1px solid #999;
      border-radius: 4px;
      font-size: 14px;
      color: #666;
    }
  }
  .btn-wrap {
    position: absolute;
    top: 55px;
    right: 20px;
    span {
      margin-left: 20px;
    }
    .edit {
      color: $base-color;
      cursor: pointer;
      &:hover {
        color: $base-color-hover;
      }
    }
  }
  .com-form {
    padding: 30px 0;
    .postForm {
      width: 500px;
      margin: 0 auto;
      .showEdit {
        padding: 0 20px 0 50px;
        .form-item {
          min-height: 40px;
          line-height: 40px;
          .delete {
            float: right;
            color: $base-color-danger;
            &:hover {
              color: $base-color-important;
              cursor: pointer;
            }
          }
        }
      }
      .form-item {
        margin-bottom: 10px;
        line-height: 40px;
        & > div {
          display: inline-block;
        }
        & > .text {
          position: relative;
          padding-left: 20px;
          min-width: 120px;
          color: #24292f;
          vertical-align: top;
          &[required] {
            &::before {
              content: '*';
              position: absolute;
              left: 0px;
              top: 0px;
              color: $base-color-danger;
            }
          }
        }
        .input {
          input {
            line-height: 28px;
            border: 1px solid #ccc;
            border-radius: 4px;
            padding: 0 10px;
            width: 350px;
            font-size: 16px;
            &:active,
            &:focus {
              border-color: $base-color;
            }
          }
        }
        label {
          margin-right: 30px;
          cursor: pointer;
        }
        .select {
          .el-input {
            width: 100%;
          }
          ::v-deep .el-input__inner {
            width: 350px;
            height: 30px;
            line-height: 30px;
            border: 1px solid #ccc;
            font-size: 16px;
            color: #24292f;
            padding-right: 10px;
            &:active,
            &:focus {
              border-color: $base-color;
            }
          }
          &.small {
            .el-input {
              width: 150px;
              display: inline-block;
            }
            ::v-deep .el-input__inner {
              width: 153px;
              padding-right: 20px;
              margin-right: 20px;
            }
            ::v-deep .el-input__suffix {
              right: 20px;
            }
          }
        }
      }
    }
  }
}
</style>
